package com.jiang.jetpackdemo.http

import com.jiang.jetpackdemo.model.ImgInfo
import com.jiang.jetpackdemo.model.VideoRecommendInfo
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Callback
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


object RetrofitManager {
    private const val BASE_URL = "https://api.apiopen.top/"

    private val RETROFIT: Retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(getClient())
        .build()

    private val RAPI: RetrofitAPI = RETROFIT.create(RetrofitAPI::class.java)
    private fun getClient(): OkHttpClient {
        val builder = OkHttpClient.Builder()
        builder.addInterceptor(getLoggingInterceptor())
        return builder.build()
    }

    private fun getLoggingInterceptor(): HttpLoggingInterceptor {
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        return loggingInterceptor
    }

    fun getVideoRecommend(id: Int, callback: Callback<VideoRecommendInfo>) {
        RAPI.getVideoRecommend("$id").enqueue(callback)
    }

    suspend fun getVideoRecommendInfo(id: Int): VideoRecommendInfo =
        RAPI.getVideoRecommendInfo("$id")


    suspend fun getPic(): ImgInfo {
        return RAPI.getRandomPic("https://api.ixiaowai.cn/api/api.php?return=json")
    }


}